﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

using EdexClient;

namespace Edex2IBS
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private EdexClient.API_SESSION session = new API_SESSION();
        private string pathString = "http://109.173.117.5/edex/";
        //private string pathString = "http://localhost:1953/edex/";



        public MainWindow()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var lastsyncdate = new DateTime(2011, 12, 30);
            string connectionString = "Data Source=ORAIBS.WIN;User ID=ibsdbo;Password=campator;";
           // string path = "http://109.173.117.5/document/download/?lastsyncdate=" + lastsyncdate.ToString("yyyy.MM.dd");
            string path = "http://localhost:1953/document/download/?lastsyncdate=" + lastsyncdate.ToString("yyyy.MM.dd");
        
            path = xmlPath.Text;



            var edex = new EdexClient.EdexContext(pathString);
            session = edex.GetSession("tnk@tnk.ru", "12345");
            edex.ChangeCompany("d2a07f89-f38e-46cb-bec8-dc38b4e8d8cb", session.APIKEY); //TNK   
            session = edex.GetSession("tnk@tnk.ru", "12345");

            var data = edex.GetIncomingInvoicesCommerceML(session.APIKEY);


            edex.ChangeCompany("E4EFEE0D-0162-4FE8-B3A8-22DFFDDBA32B", session.APIKEY); //PETROL
            session = edex.GetSession("tnk@tnk.ru", "12345");
            data = edex.GetIncomingInvoicesCommerceML(session.APIKEY);

                       

            this.process(connectionString, data);

            //var data = ibs.GetLastModifiedBPSite(lastsyncdate).ToList();
            //edex.SendLocations(data.ToList(), session.APIKEY);
            //edex.ChangeCompany("E4EFEE0D-0162-4FE8-B3A8-22DFFDDBA32B", session.APIKEY); //PETROL
            //edex.ChangeCompany("d2a07f89-f38e-46cb-bec8-dc38b4e8d8cb", session.APIKEY); //TNK
            //session = edex.GetSession("tnk@tnk.ru", "12345");




            
        }

        private void process(string connectionString, string data)
        {
            //new CommerceML2IBS
            //    .IntegrationContext(connectionString)
            //    .Process(uri, new CommerceML2IBS.Delivery { StoreId = 101 });

            new CommerceML2IBS
                .IntegrationContext(connectionString)
                .Process(data);
        }

        private void sendAuth(string userKey, string companyKey)
        {
            var edex = new EdexClient.EdexContext("http://109.173.117.5/edex/");
            edex.SetSession(userKey, companyKey);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            /// Петрол
            //sendAuth("15010a9c-9001-47b4-8014-afcd7c1b96a2", "e4efee0d-0162-4fe8-b3a8-22dffddba32b");
            GetSession("tnk@tnk.ru", "12345");
            /// ТНК
            //sendAuth("682fec8d-10b6-4daa-af00-1aecc5a6190b", "d2a07f89-f38e-46cb-bec8-dc38b4e8d8cb");
        }

        

        private void GetSession(string email, string password)
        {
            var edex = new EdexClient.EdexContext(pathString);
            session = edex.GetSession(email, password);
            
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var edex = new EdexClient.EdexContext(pathString);
            edex.ChangeCompany("E4EFEE0D-0162-4FE8-B3A8-22DFFDDBA32B", session.APIKEY);
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            //var edex = new EdexClient.EdexContext(pathString);
            //edex.GetIncommingDocuments(session.APIKEY);
            
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            //выгрузка станций
            var lastsyncdate = new DateTime(1999, 1, 1, 0, 0, 0);
            string connectionString = "Data Source=ORAIBS.WIN;User ID=ibsdbo;Password=campator;";
            var ibs = new IBS.IbsDataContext(connectionString);


            var edex = new EdexClient.EdexContext(pathString);
            session = edex.GetSession("tnk@tnk.ru", "12345");
            edex.ChangeCompany("E4EFEE0D-0162-4FE8-B3A8-22DFFDDBA32B", session.APIKEY); //PETROL

            session = edex.GetSession("tnk@tnk.ru", "12345");

            var data = ibs.GetLastModifiedBPSite(lastsyncdate).ToList();
            edex.SendLocations(data.ToList(), session.APIKEY);

            edex.ChangeCompany("d2a07f89-f38e-46cb-bec8-dc38b4e8d8cb", session.APIKEY); //TNK
            session = edex.GetSession("tnk@tnk.ru", "12345");

            var hqc = new HQC.HqcDataContext("Data Source=msk-hqdb-01.retail.tnk-bp.ru;Initial Catalog=RepDB;User Id=LinkUser; Password=ABCDabcd1234;");
            var hqcSites = hqc.GetHQCSites(lastsyncdate).ToList(); ;

            edex.SendLocations(hqcSites, session.APIKEY); 

        }


        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //выгрузка товара
            var lastsyncdate = new DateTime(1999, 10, 1, 0, 0, 0);
            string connectionString = "Data Source=ORAIBS.WIN;User ID=ibsdbo;Password=campator;";
            var ibs = new IBS.IbsDataContext(connectionString);

            var edex = new EdexClient.EdexContext(pathString);
            session = edex.GetSession("tnk@tnk.ru", "12345");
            edex.ChangeCompany("E4EFEE0D-0162-4FE8-B3A8-22DFFDDBA32B", session.APIKEY); //PETROL
            
            session = edex.GetSession("tnk@tnk.ru", "12345");

            var data = ibs.GetLastModifiedBPItems(lastsyncdate).ToList();
            edex.SendItems(data.ToList(), session.APIKEY);
            
            edex.ChangeCompany("d2a07f89-f38e-46cb-bec8-dc38b4e8d8cb", session.APIKEY); //TNK
            session = edex.GetSession("tnk@tnk.ru", "12345");

            var hqc = new HQC.HqcDataContext("Data Source=msk-hqdb-01.retail.tnk-bp.ru;Initial Catalog=RepDB;User Id=LinkUser; Password=ABCDabcd1234;");
            var hqcItem = hqc.GetHQCItems(lastsyncdate).ToList(); ;

            edex.SendItems(hqcItem, session.APIKEY); 
        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            //выгрузка поставщиков
            
            string connectionString = "Data Source=ORAIBS.WIN;User ID=ibsdbo;Password=campator;";
            var ibs = new IBS.IbsDataContext(connectionString);

            var edex = new EdexClient.EdexContext(pathString);
            session = edex.GetSession("tnk@tnk.ru", "12345");
            

            var LastSyncDateBP = new DateTime(2000, 1, 1, 0, 0, 0);
            var LastSyncDateBP_IBS = ibs.GetLastSyncDate("LastSyncDateBP_Suppliers");
            LastSyncDateBP = LastSyncDateBP > LastSyncDateBP_IBS ? LastSyncDateBP : LastSyncDateBP_IBS;
            session = edex.GetSession("tnk@tnk.ru", "12345");           
            
            edex.ChangeCompany("E4EFEE0D-0162-4FE8-B3A8-22DFFDDBA32B", session.APIKEY); //PETROL 
            session = edex.GetSession("tnk@tnk.ru", "12345");

            var sup = ibs.GetLastModifiedBPSupplier(LastSyncDateBP).ToList();
            edex.SendSupplier(sup, session.APIKEY);

            ibs.SetLastSyncDate(DateTime.Now.AddMinutes(-5).ToString("dd.MM.yyyy hh:mm:ss"), "LastSyncDateBP_Suppliers");


            //edex.ChangeCompany("d2a07f89-f38e-46cb-bec8-dc38b4e8d8cb", session.APIKEY); //TNK
            //session = edex.GetSession("tnk@tnk.ru", "12345");

            //var hqc = new HQC.HqcDataContext("Data Source=msk-hqdb-01.retail.tnk-bp.ru;Initial Catalog=RepDB;User Id=LinkUser; Password=ABCDabcd1234;");
            //var hqcSupplier = hqc.GetHQCSuppliers(lastsyncdate).ToList(); 

            //edex.SendSupplier(hqcSupplier, session.APIKEY); 
        }


        

    }
}
